package com.xywy.start.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import com.xywy.R;
import com.xywy.common.util.MLog;

/**
 * height view
 */
public class HeightView extends FrameLayout {

	Paint p;
	Paint p10;
	Paint p10font;
	Paint pRed;
	Paint pIndex;
	int halfHeight;
	int halfWidth;
	private Context mContext;
	private Handler mHandler;

	public HeightView(Context context, AttributeSet attrs) {
		super(context, attrs);
		mContext = context;
		p = new Paint();
		p.setColor(context.getResources().getColor(R.color.find_line));
		p.setStrokeWidth(6);// 笔宽5像素

		p10 = new Paint();
		p10.setColor(context.getResources().getColor(R.color.start_bt_bg));
		p10.setStrokeWidth(8);// 笔宽6像素
		p10.setTextSize(35);

		p10font = new Paint();
		p10font.setColor(context.getResources().getColor(
				R.color.start_heigth_p10));
		p10font.setStrokeWidth(8);// 笔宽6像素
		p10font.setTextSize(35);

		// 中间的横线
		pRed = new Paint();
		pRed.setColor(Color.RED);
		pRed.setStrokeWidth(2);// 笔宽2像素

		// 文字
		pIndex = new Paint();
		pIndex.setColor(Color.RED);
		pIndex.setStrokeWidth(6);// 笔宽2像素
		pIndex.setTextSize(35);
	}

	public void setHandler(Handler handler) {
		mHandler = handler;
	}

	/**
	 * 身高
	 */
	int h[] = new int[110];
	float x = 0;
	float y = 0;
	int heightIndex = 0;
	int actionIndex = 0;
	/**
	 * 最大的高 目前是绘制了200个，每个20 随意是200*20； 经过测试 如果绘制200个那么乘以199是正确的
	 */
	int maxHeight = 110 * 20;

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_UP:
			x = ev.getX();
			y = ev.getY();
			break;
		}
		return true;
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, heightMeasureSpec);

		halfHeight = getMeasuredHeight() / 2;
		halfWidth = getMeasuredWidth() / 2;
	}

	@Override
	public void computeScroll() {
		computeHeightIndex();// 这里是得到了正确的中间位置的索引
		// 这里可以写一些回调函数或具体的处理方法
	}

	int ifFrist = 0; // 标识是否是初始化

	/**
	 * 找身高索引,并将划动经过的cm发送给主线程进行显示
	 */
	public void computeHeightIndex() {

		heightIndex = (int) Math.round((halfHeight + getScrollY()) / 20.0); // 一半的高度
																			// +
																			// 滚动的高度
																			// 就是这个高度

		Message mainThreadMsg = mHandler.obtainMessage(1, (230 - heightIndex)
				+ "");
		mHandler.sendMessage(mainThreadMsg);
		// 初始定位为170
		if (ifFrist == 0) {
			// 20 代表一个刻度
			scrollTo(getScrollX(), (int) (20 * 60 - halfHeight));
		}
		ifFrist++;
	}

	int ii = 0;

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		int action = ev.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			x = ev.getX();
			y = ev.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			float x1 = ev.getX();
			float y1 = ev.getY();
			int sub = (int) (y1 - y);
			boolean isDown = sub > 0 ? true : false;
			if (sub != 0) {
				MLog.i("movePostition", "movePostition:========halfHeight："
						+ halfHeight + "    maxHeight:" + maxHeight);

				int movPositon = getScrollY() - sub;

				if (movPositon <= -halfHeight) {
					movPositon = -halfHeight;
				} else if (movPositon >= maxHeight - halfHeight) {
					movPositon = maxHeight - halfHeight;
				}

				MLog.i("movePostition", "movePostition:========" + movPositon);
				scrollTo(getScrollX(), movPositon);
			}
			x = ev.getX();
			y = ev.getY();
			ii++;
			break;
		case MotionEvent.ACTION_UP:
			Message mainThreadMsg = mHandler.obtainMessage(1,
					(230 - heightIndex) + "");
			mHandler.sendMessage(mainThreadMsg);
			break;
		}
		return true;
	}

	int ff = 0;

	@Override
	protected void onDraw(Canvas canvas) {
		for (int i = 0; i < 230; i++) {
			int y = i * 20;// 每20个像素绘制一个横线 230 这里面 i代表横线 从最高点 开始画230 220---
			// 应该是从0 开始计算他的位置
			// 我们计算的时候是要从 120 到 230
			int off = 0;
			ff = i;
			canvas.drawLine(0, y, 50 + off, y, p); // 绘制基本的线
			if (i % 5 == 0) {// 每5个像素横线变长
				off = off + 30;

				canvas.drawLine(0, y, 50 + off, y, p); // 绘制5的线
				// canvas.drawText(i+"", 70+100, y, p);
				if (i % 10 == 0) {
					off = off + 60;
					canvas.drawLine(0, y, 50 + off, y, p10); // 绘制10的线
					if (230 - i >= 120) {
						drawText(canvas, (230 - i) + "cm", 70 + 120, y,
								p10font, -90);// 绘制右侧文字
					}
				}
			}
			// 每一个距离是20 现在 *i
		}
		computeHeightIndex();
		super.onDraw(canvas);
	}

	/***
	 * 绘制角度变化text
	 * 
	 * @param canvas
	 * @param text
	 * @param x
	 * @param y
	 * @param paint
	 * @param angle
	 */
	void drawText(Canvas canvas, String text, float x, float y, Paint paint,
			float angle) {
		if (angle != 0) {
			canvas.rotate(angle, x, y);
		}
		canvas.drawText(text, x, y, paint);
		if (angle != 0) {
			canvas.rotate(-angle, x, y);
		}
	}
}